﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CMSPages;
using t_TestingLibrary;
using System.ComponentModel.Composition;
using PaperRenderingInterfaces;
using System.IO;
using DeepZoomPivotConstructor.PivotLib;
using DeepZoomPivotConstructor.VisualLib;
using System.Drawing;
using PaperPluginUtilities;
using System.Diagnostics;

namespace t_CMSPages
{
    [TestClass]
    public class t_ScanMasterCMSEventDisplayPage
    {
        [TestInitialize]
        public void TestSetup()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            Utilities.MyClassDone();
        }

        /// <summary>
        /// Load a file instead of accessing the web.
        /// </summary>
        [Export(typeof(IWebAccess))]
        private class LoadAFile : IWebAccess
        {
            public FileInfo theFileToLoad;
            public string UrlAskedFor { get; set; }

            public string GetUriAsString(Uri whatToLoad)
            {
                UrlAskedFor = whatToLoad.OriginalString;

                return LoadFile(theFileToLoad);
            }

            /// <summary>
            /// Load up the req' file.
            /// </summary>
            /// <param name="finfo"></param>
            /// <returns></returns>
            private string LoadFile(FileInfo finfo)
            {
                if (finfo.Exists)
                {
                    using (var reader = finfo.OpenText())
                    {
                        return reader.ReadToEnd();
                    }
                }
                else
                {
                    return null;
                }
            }


            public DateTime? GetLastModificationTime(Uri whatToCheck)
            {
                return DateTime.Now;
            }
        }

        /// <summary>
        /// Dummy render for CMS sub-event pages!
        /// </summary>
        [Export(typeof(IPaperRenderEngine))]
        class DummyRender : IPaperRenderEngine
        {
            public IEnumerable<PivotImage> Render(string renderName, string renderAddress)
            {
                if (renderName == "CMSEventDisplayPage")
                {
                    Trace.WriteLine("Rendering " + renderAddress);
                    yield return new PivotImage(null);
                }
            }
        }


        [TestMethod]
        public void TestCtor()
        {
            ScanMasterCMSEventDisplayPage target = new ScanMasterCMSEventDisplayPage();
        }

        [TestMethod]
        public void TestOutMasterPage()
        {
            ScanMasterCMSEventDisplayPage target = new ScanMasterCMSEventDisplayPage();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_CMSPages\\cmsMasterEventDisplayPage.html") };
            Utilities.AddPart(laf);
            Utilities.AddPart(new DummyRender());
            Utilities.Compose(target);

            var result = target.Render("http://cms.web.cern.ch/cms/Media/Images/EventDisplays/index.html");
            var results = result.ToArray();

            Assert.AreEqual(8, results.Length, "Incorrect number of images came back!");
        }
    }
}
